﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Configuration;

namespace CG67.EspaceRencontre.Outils
{
    public class BaseDeDonnee
    {

        #region BuildDataSet
        /// <summary> 
        /// Méthode qui permet de construir un DataSet à partir d'une requête 
        /// </summary> 
        /// <param name="CommandText">Requête SQL</param> 
        /// <param name="connectionString">Chaine de connexion</param> 
        /// <returns>DataSet</returns> 
        public static DataSet BuildDataSet(String CommandText, ConnectionStringSettings connectionString)
        {
            DataSet ds = null;
            DbProviderFactory factory = DbProviderFactories.GetFactory(connectionString.ProviderName);
            using (IDbConnection connection = factory.CreateConnection())
            {
                //attribution de la chaine de connexion à l'objet connection 
                connection.ConnectionString = connectionString.ConnectionString;
                connection.Open();
                //création et configuration de l'objet command 
                IDbCommand command = factory.CreateCommand();
                command.CommandText = CommandText;
                command.Connection = connection;
                IDbDataAdapter sqlAdapt = factory.CreateDataAdapter();
                sqlAdapt.SelectCommand = command;
                ds = new DataSet("dataset");
                sqlAdapt.Fill(ds);
                connection.Close();
                connection.Dispose();
            }
            return ds;
        }
        #endregion

        
        #region ExecuteNonQuery 
        /// <summary> 
        /// Méthode qui éxecute la requête passée en paramétre
        /// </summary> 
        /// <param name="CommandText">Requête SQL</param> 
        /// <param name="connectionString">Chaine de connexion</param> 
        public static void ExecuteNonQuery(String CommandText, ConnectionStringSettings connectionString) 
        { 
             
            DbProviderFactory factory = DbProviderFactories.GetFactory(connectionString.ProviderName); 
            using (IDbConnection connection = factory.CreateConnection()) 
            { 
                //attribution de la chaine de connexion à l'objet connection 
                connection.ConnectionString = connectionString.ConnectionString; 
                connection.Open(); 
                //création et configuration de l'objet command 
                IDbCommand command = factory.CreateCommand(); 
                command.CommandText = CommandText; 
                command.Connection = connection; 
                command.ExecuteNonQuery(); 
                
                connection.Close(); 
                connection.Dispose(); 
            }
        }
        #endregion
    }
}
